package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.base.ConstantController;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.LabelprintFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.EnterstorageMapper;
import com.iemes.mapper.warehouse.LabelprintMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

import net.sf.json.JSONObject;

@RequestMapping("/label_print/")
@Controller
public class Label_PrintController extends BaseController {
	
	@Inject
	private LabelprintMapper labelprintMapper;
	
	@Inject
	private EnterstorageMapper enterstorageMapper;

	// 查看标签打印表，显示按钮。
	@RequestMapping("label_print")
	public String label_print(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/label_print_list";
	}

	// 标签打印表分页
	@ResponseBody
	@RequestMapping("label_print_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		EnterstorageFormMap enterstorageFormMap = getFormMap(EnterstorageFormMap.class);
		List<Map<String, Object>> enter=enterstorageMapper.getAll(enterstorageFormMap);
		LuogicPaging(pageNow, pageSize, enter);
		pageView.setRecords(enter);
		return pageView;
	}
	
	// 编辑标签打印表
	@RequestMapping("labelprint_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据Id获得入库报表
			Map<String, Object> enter=enterstorageMapper.getById(id);
			model.addAttribute("enter",enter);
			//获得相同enterId的标签打印表
			LabelprintFormMap labelprintFormMap=getFormMap(LabelprintFormMap.class);
			labelprintFormMap.put("enterId", enter.get("id"));
			List<LabelprintFormMap> label=labelprintMapper.getEnterLabel(labelprintFormMap);
			model.addAttribute("label",label);
		}
		return Common.BACKGROUND_PATH + "/warehouse/label_print_edit";
	}

	// 修改标签打印表
	@ResponseBody			 
	@RequestMapping(value = "labelprint_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="编辑打印标签",methods="打印标签-修改打印标签") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		EnterstorageFormMap enterstorageFormMap=getFormMap(EnterstorageFormMap.class);
		//先删除
		baseMapper.deleteByAttribute("enterId","'"+Integer.parseInt(enterstorageFormMap.getStr("id"))+"'", LabelprintFormMap.class);
		// 新增标签子项
		int enterId=Integer.parseInt(request.getParameter("enterstorageFormMap.id"));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		//计算“标签打印总数”
		int total=0;
		//计算“标签退回总数”
		int returnTotal=0;
		//计算“标签损坏总数”
		int damageTotal=0;
		String[] planNo = request.getParameterValues("planNo_1");
		String[] print = request.getParameterValues("print_1");
		String[] printCount = request.getParameterValues("printCount_1");
		String[] abolish = request.getParameterValues("abolish_1");
		String[] normalCount = request.getParameterValues("normalCount_1");
		String[] differentCount = request.getParameterValues("differentCount_1");
		String[] normalEnter = request.getParameterValues("normalEnter_1");
		String[] differentEnter = request.getParameterValues("differentEnter_1");
		String[] handwork = request.getParameterValues("handwork_1");
		String[] intoCount = request.getParameterValues("intoCount_1");
		String[] totalEnter = request.getParameterValues("totalEnter_1");
		String[] produceDifferent = request.getParameterValues("produceDifferent_1");
		String[] packDifferent = request.getParameterValues("packDifferent_1");
		String[] printRemark=request.getParameterValues("printRemark_1");
		String[] returnCount=request.getParameterValues("returnCount_1");
		String[] damageCount=request.getParameterValues("damageCount_1");
		if(planNo!=null){
			for(int i = 0 ;i<planNo.length;i++){
				LabelprintFormMap labelprintFormMap=new LabelprintFormMap();
				labelprintFormMap.put("enterId", enterId);
				labelprintFormMap.put("enterCategoryId", 1);
				labelprintFormMap.put("printDate", sdf.format(date));
				labelprintFormMap.put("planNo", planNo[i]);
				labelprintFormMap.put("print", print[i]);
				if(!printCount[i].isEmpty()){
					labelprintFormMap.put("printCount", printCount[i]);
					total+=Integer.parseInt(printCount[i]);
				}
				if(!returnCount[i].isEmpty()){
					labelprintFormMap.put("returnCount", returnCount[i]);
					returnTotal+=Integer.parseInt(returnCount[i]);
				}
				if(!damageCount[i].isEmpty()){
					labelprintFormMap.put("damageCount", damageCount[i]);
					damageTotal+=Integer.parseInt(damageCount[i]);
				}
				labelprintFormMap.put("abolish", abolish[i]);
				labelprintFormMap.put("normalCount", normalCount[i]);
				labelprintFormMap.put("differentCount", differentCount[i]);
				labelprintFormMap.put("normalEnter", normalEnter[i]);
				labelprintFormMap.put("differentEnter", differentEnter[i]);
				labelprintFormMap.put("handwork", handwork[i]);
				labelprintFormMap.put("intoCount", intoCount[i]);
				labelprintFormMap.put("totalEnter", totalEnter[i]);
				labelprintFormMap.put("produceDifferent", produceDifferent[i]);
				labelprintFormMap.put("packDifferent", packDifferent[i]);
				labelprintFormMap.put("printRemark", printRemark[i]);
				labelprintFormMap.put("pFillPeopleId", userFormMap.getInt("id"));
				baseMapper.addEntity(labelprintFormMap);
				enterstorageFormMap.put("printTotal", total);
				enterstorageFormMap.put("abolishTotal",returnTotal+damageTotal);
				enterstorageFormMap.put("balance", total-returnTotal-damageTotal);
				enterstorageFormMap.put("printed", 1);
				baseMapper.editEntity(enterstorageFormMap);
			}
		}else{
			enterstorageFormMap.put("printTotal", 0);
			enterstorageFormMap.put("abolishTotal", 0);
			enterstorageFormMap.put("printed", 0);
			enterstorageFormMap.put("balance", total-returnTotal-damageTotal);
			baseMapper.editEntity(enterstorageFormMap);
		}
		return "success";
	}
}